package com.biz.primus.ms.homepage.api;

import com.biz.primus.base.vo.ProductListRestVo;
import com.biz.primus.base.vo.commodity.ProductSimpleListItemVo;
import com.biz.primus.common.utils.JsonUtils;
import com.biz.primus.homepage.enums.CoverClientType;
import com.biz.primus.homepage.vo.*;
import com.biz.primus.ms.base.api.BaseApiController;
import com.biz.primus.ms.homepage.service.CoverServerService;
import com.biz.primus.ms.homepage.service.ProgramServerService;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author jiangnan
 * @Description:
 */
@Api("首页前台接口")
@RestController
@RequestMapping("soa/frontend/homepageService")
public class HomePageFrontendApiController extends BaseApiController {

    private final CoverServerService coverServerService;

    private final ProgramServerService programServerService;

    public HomePageFrontendApiController(CoverServerService coverServerService, ProgramServerService programServerService) {
        this.coverServerService = coverServerService;
        this.programServerService = programServerService;
    }

    @ApiOperation("APP首页接口")
    @PostMapping("index")
    IndexVo index(@RequestBody HomepageReqVo reqVo) {
        IndexVo indexVo = new IndexVo();
        //获取静态数据
        CoverReqVo coverReqVo = new CoverReqVo();
        CoverTemplateData coverProgramData = coverServerService.getCoverProgramData(coverReqVo, CoverClientType.APP);
        //获取首页数据 对促销商品数据进行动态数据拼装
        CoverTemplateData fullAdv = coverProgramData.getChildren().get("fullAdv");
        indexVo.setFullAdv(new Advertisement(fullAdv.getImgUrl(), fullAdv.getLink()));

        //按钮
        List<CoverTemplateData> buttonsTemplate = coverProgramData.getChildren().get("buttons").getElements();
        List<Button> buttons = Lists.newArrayList();
        buttonsTemplate.forEach((b) -> buttons.add(new Button(b.getText(), b.getImgUrl(), b.getLink())));
        indexVo.setButtons(buttons);

        //九宫格广告
        List<CoverTemplateData> nineAdvTemplate = coverProgramData.getChildren().get("nineAdv").getElements();
        List<Advertisement> advertisements = Lists.newArrayList();
        nineAdvTemplate.forEach((n) -> advertisements.add(new Advertisement(n.getImgUrl(), n.getLink())));
        indexVo.setNineAdv(advertisements);

        List<CoverTemplateData> pagePromotionsTemplate = coverProgramData.getChildren().get("pagePromotions").getElements();
        List<PagePromotion> pagePromotions = Lists.newArrayList();
        for (CoverTemplateData index : pagePromotionsTemplate) {
            PagePromotion pagePromotion = new PagePromotion();
            List<CoverTemplateData> promotionInfos = index.getElements();
            //第一个是banner图片
            CoverTemplateData logo = promotionInfos.get(0);
            pagePromotion.setBanner(new Advertisement(logo.getImgUrl(), logo.getLink()));
            promotionInfos.remove(0);
            for (CoverTemplateData productTemp : promotionInfos) {
                ProductSimpleListItemVo list = JsonUtils.json2Obj(productTemp.getExtraData(), ProductSimpleListItemVo.class);
            }
            List<ProductListRestVo> productListRestVos = Lists.newArrayList();
            pagePromotions.add(new PagePromotion(new Advertisement(index.getImgUrl(), index.getLink()), null));
        }

        indexVo.setPagePromotions(pagePromotions);

        return indexVo;
    }


}
